import { IBizLabTemplate } from '@ibizlab/template';
import { upperFirst, kebabCase } from 'lodash';

/**
 * 导入DTOHelp助手
 *
 * @export
 * @class ImportDtoHelp
 */
export class ImportDtoHelp{
    /**
     * Creates an instance of ImportLogin.
     * @param {IBizLabTemplate} template
     * @memberof ImportDtoHelp
     */
    constructor(template: IBizLabTemplate) {
        template.engine.registerHelper('ImportDtoHelp', (...args: any[]) => {
            const appEntity = args[0];
            const dtos: any[] = appEntity.allPSAppDEMethodDTOs;
            const dtoHelp: any[] = [];
            let result: string = '';
            if (dtos && dtos.length > 0) {
                dtos.forEach((dto: any) => {
                    const dtoFields: any[] = dto.psAppDEMethodDTOFields;
                    if (dtoFields && dtoFields.length > 0) {
                        dtoFields.forEach((dtoField: any) =>{
                            if (dtoField.refPSAppDataEntity && (dtoField.refPSAppDataEntity.codeName != appEntity.codeName) && (dtoField.type == 'DTOS' || dtoField.type == 'DTO')) {
                                const index = dtoHelp.findIndex((help: any) => help.refPSAppDEMethodDTO.codeName == dtoField.refPSAppDEMethodDTO.codeName);
                                if (index == -1) {
                                    dtoHelp.push(dtoField);
                                }
                            }
                        })
                    }
                })
            }
            dtoHelp.forEach((dtoField: any) => {
                result += `${result === '' ? '' : '\n'}import { ${upperFirst(dtoField.refPSAppDEMethodDTO.codeName)}Help } from '@/api/${kebabCase(dtoField.refPSAppDataEntity.codeName)}/dto-help/${kebabCase(dtoField.refPSAppDataEntity.codeName)}-helps'`;
            })
            return result
		});
    }
}